import enum
import json
from .dd_config import *


# 消息类型
class MessageType(enum.Enum):
	text = 'text'
	link = 'link'
	markdown = 'markdown'
	actionCard = 'actionCard'
	feedCard = 'feedCard'


class Message(object):
	
	def __init__(self, msgtype: MessageType = MessageType.text):
		self.msgtype = msgtype
	
	def get_message(self, content='', text='', title='', messageUrl='', picUrl='', btnOrientation='0',
	                singleTitle='', singleURL='', btns=None, links=None, atMobiles=None, atUserIds=None, isAtAll=None):
		info = self._get_at_infos(atMobiles=atMobiles, atUserIds=atUserIds, isAtAll=isAtAll)
		message = {MSGTYPE: self.msgtype.value, ATKEY: info}
		if self.msgtype == MessageType.link:
			link = self._get_link_message(text=text, title=title, messageUrl=messageUrl, picUrl=picUrl)
			message[MESSAGE_LINK] = link
		elif self.msgtype == MessageType.markdown:
			markdown = self._get_markdown_message(text=text, title=title)
			message[MESSAGE_MARKDOWN] = markdown
		elif self.msgtype == MessageType.actionCard:
			if (singleURL == "") and (singleURL == ""):
				actionCard = self._get_actionCard_alone_message(text=text, title=title, btnOrientation=btnOrientation,
				                                                btns=btns)
			else:
				actionCard = self._get_actionCard_whole_message(text=text, title=title, btnOrientation=btnOrientation,
				                                                singleTitle=singleTitle,
				                                                singleURL=singleURL)
			message[MESSAGE_ACTIONCARD] = actionCard
		
		elif self.msgtype == MessageType.feedCard:
			feedCard = self._get_feedCard_message(links=links)
			message[MESSAGE_FEEDCARD] = feedCard
		else:
			content = self._get_text_message(content=content)
			message[MESSAGE_TEXT] = content
		return json.dumps(message)
	
	# 获取text类型消息
	def _get_text_message(self, content=''):
		message = {CONTENT: content}
		return message
	
	# 获取link类型消息
	def _get_link_message(self, text='', title='', messageUrl='', picUrl=''):
		message = {TEXT: text, TITLE: title, LINK_MESSAGE_URL: messageUrl, LINK_PIC_URL: picUrl}
		return message
	
	# 获取 markdown 类型消息(文本信息中存在Markdown语法的需要使用此消息)
	def _get_markdown_message(self, text='', title=''):
		message = {TEXT: text, TITLE: title}
		return message
	
	# 获取 actionCard_whole 类型消息
	def _get_actionCard_whole_message(self, text='', title='', btnOrientation='0', singleTitle='', singleURL=''):
		message = {TEXT: text, TITLE: title, AC_SINGLE_TITLE: singleTitle, AC_SINGLE_URL: singleURL,
		           AC_BTN_ORIENTATION: btnOrientation}
		return message
	
	# 获取 actionCard_alone 类型消息: {btns:{AC_TITLE:'',AC_ACTION_URL:''}}
	def _get_actionCard_alone_message(self, text='', title='', btnOrientation='0', btns=None):
		btns = [] if btns is None else btns
		if not isinstance(btns, list):
			btns = []
		message = {TEXT: text, TITLE: title, AC_BTN_ORIENTATION: btnOrientation, AC_BTNS: btns}
		return message
	
	# 获取 feedCard 类型消息:{FC_LINK_TITLE: "",FC_MESSAGE_URL:"",FC_PIC_URL:''}
	def _get_feedCard_message(self, links=None):
		links = [] if links is None else links
		if not isinstance(links, list):
			links = []
		message = {FC_LINKS: links}
		return message
	
	# 获取@信息
	def _get_at_infos(self, atMobiles=None, atUserIds=None, isAtAll=None):
		atMobiles = [] if atMobiles is None else atMobiles
		atUserIds = [] if atUserIds is None else atUserIds
		isAtAll = False if isAtAll is None else isAtAll
		info = {ATMOBILES: atMobiles, ATUSERIDS: atUserIds, ISATALL: isAtAll}
		return info

# 消息类型及数据格式
# text类型
# {
#     "at": {
#         "atMobiles":[
#             "180xxxxxx"
#         ],
#         "atUserIds":[
#             "user123"
#         ],
#         "isAtAll": false
#     },
#     "text": {
#         "content":"我就是我, @XXX 是不一样的烟火"
#     },
#     "msgtype":"text"
# }
#


# link类型
# {
#     "msgtype": "link",
#     "link": {
#         "text": "这个即将发布的新版本，创始人xx称它为红树林。而在此之前，每当面临重大升级，产品经理们都会取一个应景的代号，这一次，为什么是红树林",
#         "title": "时代的火车向前开",
#         "picUrl": "",
#         "messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
#     }
# }

# markdown类型
# {
#      "msgtype": "markdown",
#      "markdown": {
#          "title":"杭州天气",
#          "text": "#### 杭州天气 @150XXXXXXXX \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
#      },
#       "at": {
#           "atMobiles": [
#               "150XXXXXXXX"
#           ],
#           "atUserIds": [
#               "user123"
#           ],
#           "isAtAll": false
#       }
#  }

# 整体跳转ActionCard类型
# {
#     "actionCard": {
#         "title": "乔布斯 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
#         "text": "![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)
#  ### 乔布斯 20 年前想打造的苹果咖啡厅
#  Apple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
#         "btnOrientation": "0",
#         "singleTitle" : "阅读全文",
#         "singleURL" : "https://www.dingtalk.com/"
#     },
#     "msgtype": "actionCard"
# }


# 独立跳转ActionCard类型
# {
#     "msgtype": "actionCard",
#     "actionCard": {
#         "title": "我 20 年前想打造一间苹果咖啡厅，而它正是 Apple Store 的前身",
#         "text": "![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n\n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化，而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
#         "hideAvatar": "0",
#         "btnOrientation": "0",
#         "btns": [
#             {
#                 "title": "内容不错",
#                 "actionURL": "https://www.dingtalk.com/"
#             },
#             {
#                 "title": "不感兴趣",
#                 "actionURL": "https://www.dingtalk.com/"
#             }
#         ]
#     }
# }

# FeedCard类型
# {
#     "msgtype":"feedCard",
#     "feedCard": {
#         "links": [
#             {
#                 "title": "时代的火车向前开1",
#                 "messageURL": "https://www.dingtalk.com/",
#                 "picURL": "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
#             },
#             {
#                 "title": "时代的火车向前开2",
#                 "messageURL": "https://www.dingtalk.com/",
#                 "picURL": "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
#             }
#         ]
#     }
# }


# 常见问题
# 当出现以下错误时，表示消息校验未通过，请查看机器人的安全设置。
# // 消息内容中不包含任何关键词
# {
#   "errcode":310000,
#   "errmsg":"keywords not in content"
# }
#
# // timestamp 无效
# {
#   "errcode":310000,
#   "errmsg":"invalid timestamp"
# }
#
# // 签名不匹配
# {
#   "errcode":310000,
#   "errmsg":"sign not match"
# }
#
# // IP地址不在白名单
# {
#   "errcode":310000,
#   "errmsg":"ip X.X.X.X not in whitelist"
# }
